package com.watson.juc;

import jdk.nashorn.internal.ir.WhileNode;

import java.sql.SQLOutput;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author watson
 * @date 2021/8/2 11:10
 * @Description: 不保证原子性
 **/
public class VolatileDemo2 {
    //    private volatile static int num = 0;
    private volatile static AtomicInteger num = new AtomicInteger();

    public static void add() {
//        num++;
        num.getAndIncrement();  //
    }

    public static void main(String[] args) {
        for (int i = 1; i <= 20; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    add();
                }
            }, String.valueOf(i)).start();
        }
        while (Thread.activeCount() > 2) {
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName() + "++" + num);


    }
}
